﻿package mortal.game.view.guildNew.building
{
    import Message.DB.Tables.*;
    import com.gengine.global.*;
    import com.gengine.resource.*;
    import com.gengine.utils.*;
    import com.mui.controls.*;
    import extend.language.*;
    import flash.events.*;
    import flash.geom.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.cache.guild.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.display.*;
    import mortal.game.view.guildNew.WareHourse.*;
    import mortal.game.view.guildNew.data.*;
    import mortal.mvc.core.*;

    public class GuildNewBuildingPanel extends GSprite implements GuildNewPanelI
    {
        protected var _bg:GImageBitmap;
        protected var _midBg:GBitmap;
        protected var _guildLevel:BitmapNumberText;
        protected var _midLabel:GBitmap;
        protected var _levelBtn:GLoadedButton;
        protected var _buttomTip:GTextFiled;
        protected var _resT:GTextFiled;
        protected var _upGradeTip:GuildNewWareHAppNum;
        private var btnEnabled:Boolean = false;
        private var _cellArr:Array;
        private var _posArr:Array;
        private static var CELLNum:int = 6;
        public static var tip:GuildNewBuildTip;

        public function GuildNewBuildingPanel()
        {
            this._posArr = [new Point(-5, 194), new Point(130, 181), new Point(150, 344), new Point(302, 362), new Point(505, 270), new Point(621, 220)];
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            LoaderHelp.addResCallBack(ResFileConst.guildNBuilding, this.onResLoad);
            return;
        }// end function

        private function onResLoad() : void
        {
            this._bg = UIFactory.gImageBitmap(ImagesConst.GuildNewBuildingBg, 0, 0, this);
            this._midBg = UIFactory.gBitmap(ImagesConst.GuildNBuildingLevelBg_upSkin, 261, 217, this);
            this._guildLevel = UIFactory.bitmapNumberText(283, 231, "GuildNewBuildingNum.png", 23, 29, -8, this, 13, 1);
            this._guildLevel.text = "6";
            this._midLabel = UIFactory.gBitmap(ImagesConst.GuildNBuildingLabel, 324, 231, this);
            this._levelBtn = UIFactory.gLoadedButton(ImagesConst.GuildNBuildingLevelUpBtn_upSkin, 414, 226, 80, 36, this);
            this._levelBtn.label = "";
            this._buttomTip = UIFactory.gTextField("" + Language.getString(71423), 0, 445, 742, 20, this, GlobalStyle.textFormatHuang2.setSize(13).center());
            this._resT = UIFactory.gTextField("", 269, 192, 200, 25, this, GlobalStyle.textFormatLv.setSize(13).center());
            this._upGradeTip = UIFactory.getUICompoment(GuildNewWareHAppNum, 474, 218, this);
            this.btnEnabled = false;
            this._levelBtn.configEventListener(MouseEvent.CLICK, this.onUpHd);
            this.genCells(true);
            this.update();
            tip = UIFactory.getUICompoment(GuildNewBuildTip, 0, 0, this);
            tip.visible = false;
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            LoaderHelp.removeResEvent(ResFileConst.guildNBuilding, this.onResLoad);
            this.btnEnabled = false;
            this.genCells(false);
            EffectManager.glowFilterUnReg(this._levelBtn);
            tip = null;
            this._upGradeTip = null;
            this._bg = null;
            this._midBg = null;
            this._guildLevel = null;
            this._midLabel = null;
            this._levelBtn = null;
            this._buttomTip = null;
            this._resT = null;
            return;
        }// end function

        private function genCells(param1:Boolean) : void
        {
            var _loc_2:int = 0;
            var _loc_3:GuildNewBuildCell = null;
            if (param1)
            {
                this._cellArr = [];
                _loc_2 = 0;
                while (_loc_2 < CELLNum)
                {
                    
                    _loc_3 = UIFactory.getUICompoment(GuildNewBuildCell, 0, 0, this);
                    _loc_3.x = this._posArr[_loc_2].x;
                    _loc_3.y = this._posArr[_loc_2].y;
                    this._cellArr.push(_loc_3);
                    _loc_3.configEventListener(MouseEvent.ROLL_OVER, this.onShowTip);
                    _loc_3.configEventListener(MouseEvent.ROLL_OUT, this.hideTip);
                    _loc_2++;
                }
            }
            else
            {
                if (this._cellArr == null)
                {
                    return;
                }
                _loc_2 = 0;
                while (_loc_2 < CELLNum)
                {
                    
                    _loc_3 = this._cellArr[_loc_2];
                    _loc_3.dispose();
                    _loc_2++;
                }
                this._cellArr = null;
            }
            return;
        }// end function

        private function onShowTip(event:MouseEvent) : void
        {
            var _loc_2:GuildNewBuildCell = null;
            if (tip)
            {
                _loc_2 = event.target as GuildNewBuildCell;
                if (_loc_2)
                {
                    tip.visible = true;
                    tip.x = _loc_2.x + 50;
                    tip.y = _loc_2.y + 30;
                    tip.data = _loc_2.dataInfo;
                    this.checkY();
                }
            }
            return;
        }// end function

        private function checkY() : void
        {
            var _loc_1:* = tip.localToGlobal(new Point(0, 0));
            var _loc_2:* = tip.height - (Global.stage.stageHeight - _loc_1.y);
            if (_loc_2 > 0)
            {
                tip.y = tip.y - _loc_2;
            }
            return;
        }// end function

        private function hideTip(event:MouseEvent) : void
        {
            if (tip)
            {
                tip.visible = false;
            }
            return;
        }// end function

        public function update() : void
        {
            var _loc_2:Boolean = false;
            var _loc_1:* = Cache.instance.guild.selfGuildInfo;
            if (_loc_1.baseInfo == null)
            {
                return;
            }
            if (this._guildLevel == null)
            {
                return;
            }
            this._guildLevel.text = _loc_1.baseInfo.level.toString();
            if (_loc_1.baseInfo.level < 10)
            {
                this._guildLevel.x = 283;
            }
            else
            {
                this._guildLevel.x = 278;
            }
            if (GuildLevelTargetConfig.instance.getInfoByLevel((_loc_1.baseInfo.level + 1)) == null)
            {
                this._levelBtn.visible = false;
                this._levelBtn.toolTipData = null;
                EffectManager.glowFilterUnReg(this._levelBtn);
            }
            else
            {
                this._levelBtn.visible = true;
                this._levelBtn.toolTipData = Language.getStringByParam(71424, _loc_1.getLevelConfig().upgradeResource, (_loc_1.baseInfo.level + 1));
                _loc_2 = _loc_1.baseInfo.resource >= _loc_1.getLevelConfig().upgradeResource;
                if (_loc_2)
                {
                    this.btnEnabled = true;
                    this._levelBtn.filters = null;
                    EffectManager.glowFilterReg(this._levelBtn);
                    this._resT.htmlText = Language.getStringByParam(71425, _loc_1.baseInfo.resource, _loc_1.getLevelConfig().upgradeResource);
                    if (GuildConst.hasPermissions(GuildConst.UpgradeGuild))
                    {
                        this._upGradeTip.updateNum(1);
                    }
                    else
                    {
                        this._upGradeTip.updateNum(0);
                    }
                }
                else
                {
                    this.btnEnabled = false;
                    this._levelBtn.toolTipData = "" + Language.getString(71426);
                    EffectManager.glowFilterUnReg(this._levelBtn);
                    this._levelBtn.filters = [FilterConst.colorFilter2];
                    this._resT.htmlText = Language.getStringByParam(71427, HTMLUtil.addColor(_loc_1.baseInfo.resource.toString(), GlobalStyle.colorHong), _loc_1.getLevelConfig().upgradeResource);
                    this._upGradeTip.updateNum(0);
                }
            }
            this.updateCells(_loc_1.baseInfo.level);
            return;
        }// end function

        private function updateCells(param1:int) : void
        {
            var _loc_4:TGuildStructureConfig = null;
            var _loc_5:int = 0;
            var _loc_6:GuildNewBuildCell = null;
            var _loc_2:* = GuildNewUtil.getAllBuildByLevel(param1);
            var _loc_3:* = GuildNewUtil.typeToIndex;
            for each (_loc_4 in _loc_2)
            {
                
                if (_loc_3[_loc_4.structureType] != null)
                {
                    _loc_5 = _loc_3[_loc_4.structureType];
                    _loc_6 = this._cellArr[_loc_5];
                    _loc_6.updateInfo(_loc_4, _loc_5);
                }
            }
            return;
        }// end function

        private function onUpHd(event:MouseEvent) : void
        {
            if (this.btnEnabled == false)
            {
                return;
            }
            if (!GuildConst.hasPermissions(GuildConst.UpgradeGuild))
            {
                MsgManager.showRollTipsMsg("" + Language.getString(71428));
                return;
            }
            var _loc_2:* = Cache.instance.guild.selfGuildInfo;
            var _loc_3:* = Language.getStringByParam(71429, _loc_2.getLevelConfig().upgradeResource, (_loc_2.baseInfo.level + 1));
            Alert.show(_loc_3, "" + Language.getString(71430), Alert.OK | Alert.CANCEL, null, this.onUpdateGuildAlertSelect);
            return;
        }// end function

        private function onUpdateGuildAlertSelect(param1:int) : void
        {
            if (param1 == Alert.OK)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.GUILD_LEVEL_UP, null));
            }
            return;
        }// end function

    }
}
